package org.baicaixiaozhan.springbootwebdemo1.util;

import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * DESC: filter 工具类
 *
 * @author : baicaixiaozhan
 * @since : 2021-01-30
 */
public class FilterUtils {

    /**
     * 异常转发, filter 交给标记了 @{@link RestControllerAdvice} 的全局异常处理器来处理
     *
     * @param request
     * @param response
     * @param attrName
     * @param exception
     * @param path
     * @throws ServletException
     * @throws IOException
     */
    public static void exceptionForward(HttpServletRequest request,
                                        HttpServletResponse response,
                                        String attrName,
                                        Throwable exception,
                                        String path) throws ServletException, IOException {
        request.setAttribute(attrName, exception);
        request.getRequestDispatcher(path).forward(request, response);
    }
}
